﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgNetComponentsDx.Chat
{
    public class ChatMessageInfo
    {
        public void ParseMessage(byte[] bytes)
        {
            ParseMessage(ChatTools.GetString(bytes));
            if (Type == ChatMessages.File || Type == ChatMessages.FileEnd)
            {
                int len = ChatTools.GetBytes(FormatMessage()).Length;
                FileBytes = bytes.Skip(len).ToArray();
            }
        }
        public void ParseMessage(byte[] bytes, int count)
        {
            ParseMessage(ChatTools.GetString(bytes, count));
        }
        public void ParseMessage(string fullMessage)
        {
            Dictionary<int, string> parts = fullMessage.GetFirstMatch(ChatMessages.FullMessageMask, new int[] { 1, 2, 3 }, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
            if (parts != null)
            {
                Type = parts[1];
                User = parts[2];
                Message = parts[3];
                
            }
        }
        
        public string FormatMessage()
        {
            string msg = ChatMessages.FullMessage.XFormat(Type, User, Message);
            return msg;
        }

        public string Type { get; set; }
        public string User { get; set; }
        public string Message { get; set; }
        public byte[] FileBytes { get; set; }

        public override string ToString()
        {
            return "Type: " + Type + "; User: " + User + "; Message: " + Message;
        }
    }

}
